/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tramite.bc;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tesoreria.be.EnumEstadoSerie;
import org.cip.esymons.tramite.be.TipoDocumento;

/**
 *
 * @author Freddy Infantes
 */
@Stateless
public class TipoDocumentoFacade implements TipoDocumentoFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(TipoDocumento tipoDocumento) {
        em.persist(tipoDocumento);
    }

    public void edit(TipoDocumento tipoDocumento) {
        em.merge(tipoDocumento);
    }

    public void remove(TipoDocumento tipoDocumento) {
        TipoDocumento aux = em.merge(tipoDocumento);
        em.remove(aux);
    }

    public Long obtenerCodigoDocPago(String docpago){
        try{
            Query q=em.createNativeQuery("select tdo_codigo from tra_tipo_documento where tdo_desc =?1");
            q.setParameter(1, docpago);
            Vector v = (Vector) q.getSingleResult();
             //return (Long)v.get(0);
            return ((BigDecimal) v.get(0)).longValue();
        }catch(Exception ex){
            return null;
        }
    }

    public TipoDocumento find(Object id) {
        try {
            return em.find(TipoDocumento.class, id);
        } catch (Exception e) {
            return null;
        }
    }
    
    public List<TipoDocumento> findAll() {
        try {
            return em.createQuery("select object(o) from TipoDocumento as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<TipoDocumento> listarDocumentosPagoXIngreso() {
        try {
            return em.createQuery("select object(o) from TipoDocumento as o where o.tdoVerEnTesoreriaIngresos=true").getResultList();
        } catch (Exception e) {
        }   return new ArrayList();
    }

     public TipoDocumento ubicarUnDocPagoXIngresoPorNombre(String nombreTipoDoc) {
        try {
            return (TipoDocumento)em.createQuery("select object(td) from TipoDocumento as td where td.tdoVerEnTesoreriaIngresos=true and td.tdoDesc=?1").setParameter(1, nombreTipoDoc).getSingleResult();
        } catch (Exception e) {
        }   return null;
    }

    public List<TipoDocumento> listarDocPagoTesoreriaSinSeriesVigentes() {
        try {
            return em.createQuery("select sdp.tdoCodigo from SerieDocPago as sdp where sdp.tdoCodigo.tdoCodigo not in (select sd.tdoCodigo.tdoCodigo from SerieDocPago as sd where sd.serEstado=?1)").setParameter(1, EnumEstadoSerie.VIGENTE).getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }
    
    public List<TipoDocumento> listarDocumentosXEgresos() {
        try {
            return em.createQuery("select object(o) from TipoDocumento as o where o.tdoVerEnTesoreriaEgresos=true").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<TipoDocumento> listarDocumentosParaTesoreria() {
        try {
            return em.createQuery("select object(o) from TipoDocumento as o where o.tdoVerEnTesoreriaIngresos=true or o.tdoVerEnTesoreriaEgresos=true").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<TipoDocumento> listarDocumentosParaTramite() {
        try {
            return em.createQuery("select object(o) from TipoDocumento as o where o.tdoVerEnTramite=true").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<TipoDocumento> buscarTipoDocumento(String nombre) {
        return em.createQuery("select object(o) from TipoDocumento as o where o.tdoDesc like ?1").setParameter(1, "%" +nombre+ "%").getResultList();
    }

    public TipoDocumento buscarXNombre(String nombre) {
        try{
        return (TipoDocumento) em.createQuery("select object(o) from TipoDocumento as o where o.tdoDesc = ?1").setParameter(1, nombre).getSingleResult();
        }catch(Exception e){
            return null;
        }
    }

    public List<TipoDocumento> paginadorTipoDocumento(int cantidad, int posicion,String desc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select td From TipoDocumento td ");


            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where td.tdoDesc like ?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, "%" + desc + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorTipoDocumento( String desc) {
       try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select COUNT(td) From TipoDocumento td ");

            if (desc != null && !desc.isEmpty()) {
                sb.append(" Where td.tdoDesc =?1");
            }
            q = em.createQuery(sb.toString());

            if (desc != null && !desc.isEmpty()) {
                q.setParameter(1, desc);
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }
}

